package com.hzlx.service;

import com.hzlx.bean.User;
import com.hzlx.dao.UserDao;
import com.hzlx.dto.UserDto;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class UserService {
    @Resource
    UserDao userDao;
    public UserDto findByUserName(String account) {
        User user = userDao.loadByAccount(account);
         Stream<String> s1 = user.getPermissionList().stream().map(perm->perm.getPerm());
        Stream<String> s2 = user.getRoleList().stream().map(role -> "role:"+role.getRoleCode());
        //将是s1,s2拼接并转换为数组————String[]
        List<String> authorities = Stream.concat(s1,s2)
                .filter(au->au!=null)
                .collect(Collectors.toList());
        return UserDto.builder()
                .id(user.getId())
                .username(user.getUsername())
                .account(user.getAccount())
                .password(user.getPassword())
                .authorities(authorities)
                .build();
    }
}
